<template>
  <j-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="取消"
    wrapClassName="ant-modal-cust-warp"
    style="top:5%;height: 100%;overflow-y: hidden">
    <template slot="footer">
      <a-button key="back" v-if="isReadOnly" @click="handleCancel">
        取消
      </a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form" style="width: 50%;margin: 0 auto;">
        <a-form-item  label="登录名" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input placeholder="请输入登录名"  :disabled="disabled" v-decorator.trim="[ 'loginName', validatorRules.loginName]"/>
        </a-form-item>
        <a-form-item  label="公司名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input placeholder="请输入公司名称" :disabled="disabled"  v-decorator.trim="[ 'name', validatorRules.name]"/>
        </a-form-item>
       <a-form-item label="法人" :labelCol="labelCol" :wrapperCol="wrapperCol" >
        <a-input placeholder="请输入法人" :disabled="disabled"  v-decorator.trim="[ 'legal', validatorRules.legal]"/>
       </a-form-item>
       <a-form-item label="电话号码" :labelCol="labelCol" :wrapperCol="wrapperCol" >
         <a-input placeholder="请输入电话号码" :disabled="disabled"  v-decorator.trim="[ 'tel', validatorRules.tel]"/>
       </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="营业执照">
            <j-image-upload v-model="fileList"  :disabled="disabled"  v-decorator.trim="[ 'licence', validatorRules.licence]" bizPath="business" text="上传" isMultiple></j-image-upload>
        </a-form-item>
        <div>
        </div>
      </a-form>
    </a-spin>
  </j-modal>
</template>
<script>
  import pick from 'lodash.pick'
  import Vue from 'vue'
  import { FormTypes } from '@/utils/JEditableTableUtil'
  import JSelectPosition from '@/components/jeecgbiz/JSelectPosition'
  import JDate from '@/components/jeecg/JDate'
  import { ACCESS_TOKEN } from "@/store/mutation-types"
  import { editCustom, getTenantInfoById,getConfigSourceList} from '@/api/api'
  import JImageUpload from '../../../components/jeecg/JImageUpload'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
import { addTenant,editTenant } from '../../../api/api'
  export default {
    name: "EngineerModal",
    mixins: [JEditableTableMixin],
    components: {
      JImageUpload,
      JSelectPosition,
      JImageUpload,
      JDate
    },
    data () {
      return {
        sourceList: [],
        disabled:false,
        opeList:[],
        title:"操作",
        visible: false,
        operatorList:[],
        refKeys: ['materialDataTable', ],
        tableKeys:['materialDataTable', ],
        activeKey: 'materialDataTable',
        modalWidth:800,
        drawerWidth:700,
        // credit_options : ["风险", "正常", "优秀", "极好","黑名单"],
        fileList:[],
        id:"", //保存工程id
        isReadOnly: false,
        disableSubmit:false,
        dateFormat:"YYYY-MM-DD",
        validatorRules:{
          name:{
            rules: [{
              required: false, message: '请输入公司名'
            }]
          },
          loginName:{
            rules: [
              {
              required: true, message: '请输入登录名'
            }
            ]
          },
          licence:{
            rules: [
              {
              required: false, message: '请输入营业执照'
            }
            ]
          },
          legal:{
            rules: [
              {
              required: false, message: '请输入法人'
            }
            ]
          },
          tel:{
            rules: [{
              required: false, message: '请输入电话号码', type: 'string', trigger: 'blur',
               transform (value) {
                if(value == '' || value == null){
                  return value;
                }
                // 电话号码的正则
                let reg = /^1[3456789]\d{9}$/
                if (reg.test(value)) {
                  return value;
                } else {
                  return false;
                 }
               }
            }]
          }
        },
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        wrapperCol1: {
          xs: { span: 24 },
          sm: { span: 7 },
        },
        uploadLoading:false,
        // confirmLoading: false,
        headers:{},
        // form:this.$form.createForm(this)
      }
    },
    created () {
      const token = Vue.ls.get(ACCESS_TOKEN);
      this.headers = {"X-Access-Token":token}
    },
    methods: {
    show(record){
        this.visible = true;
        let params = {
          id : record.id
        }
        this.disabled = true
        let that = this
        // 通过获取单个数据的接口(/custom/selectOne)1
        getTenantInfoById(params).then(res => {
              console.log(res,'res');
              // console.log(res.data,'getCustomOne');
              that.model = Object.assign({}, res.data);
          that.form.setFieldsValue(pick(that.model,'name','loginName','licence','legal','tel'))
            })
      },
      add () {
        // this.materialTable.dataSource.splice(0,this.materialTable.dataSource.length)
        this.edit({});
      },
      edit (record) {
        let that = this;
        this.disabled = false
        that.form.resetFields();
        that.id = record.id;
        that.visible = true;
        let params = {
          id : record.id
        }
        if(record.id == null){
          return
        }
    // 通过获取单个数据的接口(/custom/selectOne)
    getTenantInfoById(params).then(res => {
          console.log(res,'res');
          that.model = Object.assign({}, res.data);
          that.form.setFieldsValue(pick(that.model,'name','loginName','licence','legal','tel'))
        })
      },
      close() {
        this.$emit('close');
        this.visible = false;
        this.disableSubmit = false;
      },
      loadData(){
        this.$emit('save');
      },
      classifyIntoFormData(allValues) {
        let billMain = Object.assign(this.model, allValues.formValue)
        let detailArr = allValues.tablesValue[0].values
        if(this.model.id){
          billMain.id = this.model.id
        }
        return {
          info: JSON.stringify(billMain),
          rows: JSON.stringify(detailArr),
        }
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let formData = Object.assign(this.model, values);
            that.confirmLoading = true;
            let obj;
            if(!this.model.id){
              formData.id = this.id;
              obj=addTenant(formData);
            }else{
              obj=editTenant(formData);
            }
            obj.then((res)=>{
              if(res.code === 200){
                that.$emit('ok');
              }else{
                that.$message.warning(res.data.message);
              }
            }).finally(() => {
              that.loadData()
              that.confirmLoading = false;
              that.close();
            })
          }
          })
      },
      handleCancel() {
        this.close()
      },
    }
  }
</script>

<style scoped>

</style>